import CommonDrawer from '@/components/CommonDrawer';
import { Descriptions } from 'antd';
import dayjs from 'dayjs';

const Detail = (props) => {
  const { open, record, onCancel } = props;

  // 格式化数据用于展示
  const formatData = (data) => {
    if (!data) return {};

    return {
      ...data,
      firstUseDate: data.firstUseDate
        ? dayjs(data.firstUseDate).format('YYYY-MM-DD')
        : '-',
      time: data.time ? dayjs(data.time).format('YYYY-MM-DD HH:mm:ss') : '-',
      remark: data.remark || '-',
      type:
        data.type !== undefined
          ? data.type === 0
            ? '磷酸铁锂电池'
            : data.type === 1
            ? '锂电池'
            : '-'
          : '-',
    };
  };

  const formattedRecord = formatData(record);

  const items = [
    {
      key: 'license',
      label: '车牌号',
      children: record?.license || '-',
    },
    {
      key: 'bmsCode',
      label: 'BMS编码',
      children: record?.bmsCode || '-',
    },
    {
      key: 'type',
      label: '类型',
      children: formattedRecord.type,
    },
    {
      key: 'ratedCapacity',
      label: '额定容量(Ah)',
      children: record?.ratedCapacity || '-',
    },
    {
      key: 'ratedEnergy',
      label: '额定能量(kWh)',
      children: record?.ratedEnergy || '-',
    },
    {
      key: 'mader',
      label: '生产厂商',
      children: record?.mader || '-',
    },
    {
      key: 'batteryCells',
      label: '电芯数',
      children: record?.batteryCells || '-',
    },
    {
      key: 'temperatureProbes',
      label: '温度探针数',
      children: record?.temperatureProbes || '-',
    },
    {
      key: 'firstUseDate',
      label: '首次使用日期',
      children: formattedRecord.firstUseDate,
    },
    {
      key: 'soc',
      label: 'SOC(%)',
      children: record?.soc || '-',
    },
    {
      key: 'soh',
      label: 'SOH(%)',
      children: record?.soh || '-',
    },
    {
      key: 'voltages',
      label: '总电压(V)',
      children: record?.voltages || '-',
    },
    {
      key: 'amperages',
      label: '总电流(A)',
      children: record?.amperages || '-',
    },
    {
      key: 'totalChargeValue',
      label: '累计充电量(kWh)',
      children: record?.totalChargeValue || '-',
    },
    {
      key: 'totalDischargeValue',
      label: '累计放电量(kWh)',
      children: record?.totalDischargeValue || '-',
    },
    {
      key: 'coolType',
      label: '冷却方式',
      children: record?.coolType || '-',
    },
    {
      key: 'latitude',
      label: '纬度',
      children: record?.latitude || '-',
    },
    {
      key: 'longitude',
      label: '经度',
      children: record?.longitude || '-',
    },
    {
      key: 'remark',
      label: '备注',
      children: formattedRecord.remark,
    },
  ];

  return (
    <CommonDrawer
      title="详情"
      open={open}
      onClose={onCancel}
      width={1000}
      destroyOnHidden
      footer={null}
    >
      <h3>电池信息</h3>
      <Descriptions
        items={items}
        column={3}
        bordered
        size="middle"
        labelStyle={{
          fontWeight: 'bold',
          width: '120px',
          textAlign: 'right',
        }}
        contentStyle={{
          minHeight: '32px',
          padding: '8px 12px',
        }}
      />
    </CommonDrawer>
  );
};

export default Detail;
